import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)
const store = new Vuex.Store({
    state: {
        clickflag:true,
        //付款的索引
        fukuanindex: {},
        //购物车数量初始值
        number: 1,
        //详情页面购物车 数量显示 状态
        flag: false,
        // 分类页面侧边栏索引
        sidebarindex: 0,
        // 分类页面搜索框关键词
        keyword: "下装",
        // 首页好物推荐商品信息
        goodsinfo: {

        },
        shopcart: {},
        //选择状态
        selectflag: false,

        // addressList: [],

        //收货地址信息
        // chosenAddressId: JSON.parse(localStorage.getItem('addressdata')).length + 1 - 1,
        chosenAddressId: 1,
        userflag: false,
        username: '',
        //滑动验证状态
        confirmSuccess: false,
        shop: [],

        //支付完成  待发货数据存储地
        gowushuju: [

            {
                id: 1,
                name: '添恩特毛巾44444',
                imgUrl: "../../../static/images/category/homeclass07.png",
                title: '测试2',
                pricenum: '25*50*1条装',
                num: 1,
                price: 8.9,
                allnumprice: "共1件 实际付款8.9"
            }, {
                id: 2,
                name: '添恩特毛巾',
                imgUrl: "../../../static/images/category/homeclass07.png",
                title: '测试2',
                pricenum: '25*50*1条装',
                num: 1,
                price: 8.9,
                allnumprice: "共1件 实际付款8.9"

            }
        ],
        datas: [],
        cartshop: [],
        allchecked: false, //全选按钮状态
        dianpuchecked: false,
        datas: [],
        fukuandata: {},
        detailcount: 0
    },
    getters: {

    },
    mutations: {
        fukuanindexFn(state, step) {
            state.fukuanindex = step;
        },
        detailcountFn(state) {
            let arr = JSON.parse(localStorage.getItem('onshop'))
            if (!arr) {
                return
            } else {
                console.log(arr);
                let count = 0;
                arr.forEach(item => {
                    count += item.num;
                })
                state.detailcount = count;
            }
            // state.detailarr = arr;

        },
        changetypestatus(state) {
            state.selectflag = false;
        },
        fukuan(state, step) {
            state.fukuandata = step;
        },
        showshuju(state, step) {
            state.cartshop = step;
            console.log(state.cartshop);
        },
        editdata(state, step) {
            // console.log(state.chosenAddressId);

        },
        userflag(state) {
            state.userflag = true
            if (state.userflag == true) {
                state.username = JSON.parse(sessionStorage.getItem('uname')).uname;
            }

        },
        huadong(state) {
            state.confirmSuccess = true;
        },
        //购物车加
        add(state, index) {
            state.cartshop[index].num++
        },
        //购物车减
        reduce(state, index) {
            state.cartshop[index].num--
        },
        //全选和单选
        quanxuan(state) {
            console.log(222);
            state.allchecked = !state.allchecked;
            console.log(state.allchecked);
            for (let i = 0; i < state.cartshop.length; i++) {
                if (state.dianpuchecked == true) {
                    state.cartshop[i].checked = true
                    state.dianpuchecked = true
                } else {
                    state.cartshop[i].checked = false
                    state.dianpuchecked = false
                }
            }
        },
        danxuan(state) {
            state.cartshop.filter(item => {
                if (item.checked == false) {
                    // state.dianpuchecked = false
                    state.dianpuchecked = false
                }
                return item.checked == true
            }).length === state.cartshop.length ? state.dianpuchecked = true : false


        },
        //去支付的判断
        goorder(state) {
            // for (let i = 0; i < state.cartshop.length; i++) {
            //     if (state.cartshop[i].checked == false) {
            //         alert("请先选中商品");
            //         console.log(222);
            //     } else {
            //         this.$router.push('/order')
            //     }

            // }
            // console.log(222);
        },
        remove(arr, val) {
            let index = arr.indexOf(val);
            if (index > -1) {
                arr.splice(index, 1)
            }
        },
        //购物车选择的内容
        shopcartcontent(state, obj) {
            state.shopcart = obj
                // console.log("state.shopcart", state.shopcart);
        },
        // 分类页面索引
        changeindex(state, index) {
            state.sidebarindex = index
                // console.log(state.sidebarindex);
        },
        // 获取关键词
        getKeyWord(state, params) {
            if (params == "新鲜水果") {
                state.keyword = "水果";
            } else {
                state.keyword = params;
            }


        }
    },
    getters: {
        showNum(state) {
            let count = 0;
            state.cartshop.forEach(item => {
                if (item.checked == true) {
                    count += item.num
                }

            })
            return count;
        },
        showsunprice(state) {
            let sumprice = 0;
            state.cartshop.forEach(item => {
                if (item.checked == true) {
                    sumprice += item.num * item.price;
                }
            })
            return sumprice;

        },
        sunprice(state) {
            return state.fukuandata.price * state.fukuandata.num;
        },
    }
})

export { store }